﻿using System;
using System.Collections;
using System.Collections.Generic;
using com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx;
using UnityEngine;
using UnityEngine.UI;

namespace com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx
{
    public class AdaptBaseConfig
    {
        /// <summary>
        /// 未记录的属性默认值,当读取属性为默认值时，则不进行属性修改
        /// -1 为LayoutElementField 关闭时的默认值
        /// </summary>
        protected const int DEFAULT_VALUE = -10000;
    }

    public class AdaptBase<T, T1> : ScreenOrientationChangeHandler where T : new() where T1 : Component
    {
        /// <summary>
        /// 未记录的属性默认值,当读取属性为默认值时，则不进行属性修改
        /// -1 为LayoutElementField 关闭时的默认值
        /// </summary>
        protected const int DEFAULT_VALUE = -10000;
        
        //1:横屏 2:竖屏
        [SerializeField]
        public int currentConfigIndex = 0;
        
        [ContextMenuItem("保存到当前配置", "Copy2LandscapeConfig")]
        [ContextMenuItem("使用配置属性", "ApplyLandscapeConfig")]
        [SerializeField]
        protected T LandscapeConfig;

        [ContextMenuItem("保存到当前配置", "Copy2PortraitConfig")]
        [ContextMenuItem("使用配置属性", "ApplyPortraitConfig")]
        [SerializeField]
        protected T PortraitConfig;
        
        protected T CurrentConfig {
            get {
                return IsLandscape() ? LandscapeConfig : PortraitConfig;
            }
        }
        private T1 _mComponent;
        
        protected T1 mComponent {
            get {
                if (null == _mComponent) {
                    _mComponent = GetComponent<T1>();
                }
                return _mComponent;
            }
        }
        
        protected virtual void Reset() {
            Copy2LandscapeConfig();
            Copy2PortraitConfig();
        }
        
        protected void Copy2LandscapeConfig() {
            if (null == LandscapeConfig) {
                LandscapeConfig = new T();
            }
            CopyProperty(LandscapeConfig);
        }

        protected void Copy2PortraitConfig() {
            if (null == PortraitConfig) {
                PortraitConfig = new T();
            }
           
            CopyProperty(PortraitConfig);
        }

        public override void SetInvalid()
        {
            currentConfigIndex = -1;
        }

        //强制Apply
        public override void ApplyLandscapeConfig()
        {
            base.ApplyLandscapeConfig();
            if (null == LandscapeConfig || mComponent == null) {
                return;
            }

            currentConfigIndex = 1;
            internalApplyConfig(LandscapeConfig);
            
        }
        //强制Apply
        public override void ApplyPortraitConfig()
        {
            base.ApplyPortraitConfig();
            if (null == PortraitConfig || mComponent == null ) {
                return;
            }
            currentConfigIndex = 2;
            internalApplyConfig(PortraitConfig);
        }
        private void internalApplyConfig(T config) {
            try
            {
                ApplyConfig(config);
            }
            catch (Exception e)
            {
                Debug.LogError("ApplyConfig Error:" + e);
            }
            
        }
        public override bool SaveConfigData()
        {
            if (IsLandscape())
            {
                if (ScreenOrientManager.GetInstance().LanscapeOnlyRead)
                {
                    UnityEngine.Debug.LogError("横屏模式下锁定为只读，不允许保存配置，请解锁后再保存配置");
                    return false;
                }
                Copy2LandscapeConfig();
               
            }else if(IsyPortrait())
            {
                if (ScreenOrientManager.GetInstance().PortraitOnlyRead)
                {
                    UnityEngine.Debug.LogError("竖屏模式下锁定为只读，不允许保存配置，请解锁后再保存配置");
                    return false;
                }
                Copy2PortraitConfig();
            }
            else
            {
                UnityEngine.Debug.LogError("未知屏幕朝向,保存配置失败!!!!!!");
                return false;
            }

            return true;
        }
        protected virtual void ApplyConfig(T config) {
            
        }

        protected virtual void CopyProperty(T config) {

        }
        
        public override void ScreenOrientationChange(UnityEngine.ScreenOrientation orientation,bool needForceApply = false)
        {
            if(!enabled || mComponent==null) return;
            if (orientation == UnityEngine.ScreenOrientation.LandscapeLeft || orientation == UnityEngine.ScreenOrientation.LandscapeRight)
            {
                if (null == LandscapeConfig) {
                    return;
                }

                if (currentConfigIndex != 1 || needForceApply)
                {
                    internalApplyConfig(LandscapeConfig);
                    currentConfigIndex = 1;
                }else
                { 
                    //Debug.LogWarning("====当前朝向已经是横屏模式=====");
                }
               
            }
            else if(orientation == UnityEngine.ScreenOrientation.Portrait || orientation == UnityEngine.ScreenOrientation.PortraitUpsideDown)
            {
                if (null == PortraitConfig) {
                    return;
                }
                if(currentConfigIndex != 2  || needForceApply)
                {
                    internalApplyConfig(PortraitConfig);
                    currentConfigIndex = 2;
                }
            }
            else
            {
                Debug.LogError("未知的屏幕方向");
            }
            
        }
        
        public override bool IsCanCleanup()
        {
            if (null == LandscapeConfig || null == PortraitConfig) {
                return false;
            }
            if (LandscapeConfig.Equals(PortraitConfig))
            {
                return true;
            }
            
            return false;
        }
#if UNITY_EDITOR
        public override bool HasComponent()
        {
            return mComponent != null;
        }
#endif
        

    }
}
